package com.cx.controller;


import com.cx.pojo.Goods;
import com.cx.pojo.OrderGoods;
import com.cx.pojo.Orders;
import com.cx.pojo.Users;
import com.cx.service.MyOrderService;
import com.cx.vo.CartsInfo;
import com.cx.vo.OrderGoodVO;
import com.cx.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/order")
public class MyOrderController {

  @Autowired
    MyOrderService myOrderService;

    /**
     * 查看订单
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/seeOrder")
    public String orders(HttpSession session, Model model){

        Users user = (Users) session.getAttribute("user");
        if (user == null) {
            model.addAttribute("msg", "请登录后查看订单!");
            return "login";
        }


        //根据用户id获取订单列表
        List<Orders> ordersList =myOrderService.getListByUserid(user.getId());

        List<OrderVO> orderVOList=new ArrayList<>();
        for (Orders orders : ordersList) {
            OrderVO orderVO = new OrderVO();
            List<OrderGoodVO> orderGoodVOList = new ArrayList<>();
            List<OrderGoods> orderGoodsList = myOrderService.getListByOrderId(orders.getId());
            for (OrderGoods orderGoods : orderGoodsList) {
                OrderGoodVO orderGoodVO = new OrderGoodVO();
                Goods goods =  myOrderService.selectGoodsByGoodId(orderGoods.getGoodId());
                orderGoodVO.setOrderId(orderGoods.getOrderId());
                orderGoodVO.setGoodNum(orderGoods.getGoodNum());
                orderGoodVO.setGoodName(goods.getGoodName());
                orderGoodVO.setGoodImage(goods.getGoodImages());
                orderGoodVO.setGoodPrice(goods.getGoodPrice());
                orderGoodVOList.add(orderGoodVO);
            }

            orderVO.setOrderId(orders.getId());
            orderVO.setOrderNo(orders.getOrderNo());
            orderVO.setOrderStatus(orders.getOrderStatus());
            orderVO.setTotalMoney(orders.getTotalMoney());
            orderVO.setObject(orderGoodVOList);
            orderVO.setCreateTime(orders.getCreateTime());
            orderVOList.add(orderVO);

        }

        model.addAttribute("orderVOList",orderVOList);







        return "order";
    }









}
